package org.campuscontent.ui;

import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.util.MovableEditor;

public class Template8 extends TemplateBase 
{
	public static int paraID = 8;

	public JLabel jlbQuiz;
	public MovableEditor jtfAnswer;
	
	private JButton ButtonForward = new JButton();
    private JButton ButtonBack = new JButton();
    private JButton ButtonBegin = new JButton();
    private JButton ButtonEnd = new JButton();
    
	String graphStrTemp;
	
    int strPointer	= 0;
    
    int startPnt, endPnt;


	public Template8(MainFrame mf)
	{
		super(mf);

		jtfAnswer = new MovableEditor();
//		jtfAnswer.addKeyListener(this);
		jtfAnswer.setBorder(BorderFactory.createRaisedBevelBorder());
		jtfAnswer.setContentType("text/html");
		
		jlbQuiz = new JLabel("");
		jlbQuiz.setText(Config.Tp8Label_DE);

		
	    ImageIcon iconBegin   = new ImageIcon(this.getClass().getResource("/org/campuscontent/images/toolbar/begin.gif"));
	    ImageIcon iconBack    = new ImageIcon(this.getClass().getResource("/org/campuscontent/images/toolbar/back.gif"));
	    ImageIcon iconForward = new ImageIcon(this.getClass().getResource("/org/campuscontent/images/toolbar/forward.gif"));
	    ImageIcon iconEnd     = new ImageIcon(this.getClass().getResource("/org/campuscontent/images/toolbar/end.gif"));
	    
	    ButtonBegin.setIcon(iconBegin);
	    ButtonBack.setIcon(iconBack);
	    ButtonForward.setIcon(iconForward);
	    ButtonEnd.setIcon(iconEnd);
	    
	    ButtonForward.setBorderPainted(false);
        ButtonBack.setBorderPainted(false);
        ButtonBegin.setBorderPainted(false);
        ButtonEnd.setBorderPainted(false);
        
//        ButtonForward.setBackground(Config.ColorPanelButtons);
//        ButtonBack.setBackground(Config.ColorPanelButtons);
//        ButtonBegin.setBackground(Config.ColorPanelButtons);
//        ButtonEnd.setBackground(Config.ColorPanelButtons);
        
		ButtonBegin.setBounds(450, 40, 50, 25);
		ButtonBack.setBounds(500, 40, 50, 25);
		ButtonForward.setBounds(550, 40, 50, 25);
		ButtonEnd.setBounds(600, 40, 50, 25);
		
		ButtonBegin.setActionCommand("Begin");
		ButtonBack.setActionCommand("Back");
		ButtonForward.setActionCommand("Forward");
		ButtonEnd.setActionCommand("End");
		
		ButtonBegin.addActionListener(this);
		ButtonBack.addActionListener(this);
		ButtonForward.addActionListener(this);
		ButtonEnd.addActionListener(this);
		
		jlbQuiz.setBounds(5, 5, 400, 30);
		jtfAnswer.setBounds(30, 40, 390, 30);

		left.add(jlbQuiz);
		left.add(jtfAnswer);
		left.add(ButtonBegin);
		left.add(ButtonBack);
		left.add(ButtonForward);
		left.add(ButtonEnd);
		
		//set the initial state to be highlighted
		

	}

	public MovableEditor getMovableEditor()
	{
		return jtfAnswer;
	}
	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
	}

	public String getAnswer()
	{
		sAnswer = jtfAnswer.getText().trim();
		return sAnswer;
	}

	public String getParaConfig()
	{
		return paraConfig;
	}

	public void setParaConfig(String config)
	{
	}

	public boolean setPreview()
	{
		if (!super.setPreview()) return false;
		
		graphStrTemp = mf.ae.getGraphAsString();
		
		strPointer	= 0;
		
		return true;
	}

	public boolean exitPreview()
	{
		super.exitPreview();
		mf.ae.loadGraphFromStr(graphStrTemp);
		return true;		
	}

	public String checkAns()
	{
		while(strPointer < jtfAnswer.getNetString().length())
		{
			String input	= nextInput();
			if(graphInspector.nextHighlight(input))
			{
				jtfAnswer.forward(startPnt, endPnt);
			}
			else
			{				
				jtfAnswer.moveToEnd(startPnt, Config.ColorFinished, Config.ColorWrong);
				return "The given string can not be recognized by the Automata!";
			}						
		}
		
		if (graphInspector.isEndState())
			return "True";
		else
			return "The given string can not reach an end status";
	}

	public void showSolution()
	{
		super.showSolution();
		
		jtfAnswer.setText(graphInspector.getRefStr()); // TODO: peng
	}

	public int getId()
	{
		return paraID;
	}
	
	public void actionPerformed(ActionEvent event)
	{
		super.actionPerformed(event);
		
		String cmd = event.getActionCommand();
		
		if(cmd.equals("Begin"))
		{
			jtfAnswer.moveToBegin();
			
			while(strPointer > 0)
			{
				String input	= previousInput();
				graphInspector.previousHighlight(input);
			}
		}
		else if(cmd.equals("Back"))
		{
			if(strPointer > 0)
			{
				jtfAnswer.back(startPnt);
				String input	= previousInput();
				graphInspector.previousHighlight(input);													
			}
			else
			{
				JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You have already reached begin of the string!");
			}
		}
		else if(cmd.equals("Forward"))
		{	
			if(strPointer < jtfAnswer.getNetString().length())
			{
				String input	= nextInput();
				if(graphInspector.nextHighlight(input))
				{
					jtfAnswer.forward(startPnt, endPnt);
				}
				else
				{
					JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "The current string can not be accepted the automata");
					jtfAnswer.moveToEnd(startPnt, Config.ColorFinished, Config.ColorWrong);
				}						
			}
			else
			{
				JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You have already reached end of the string!");
			}
		
		}
		else if(cmd.equals("End"))
		{
			while(strPointer < jtfAnswer.getNetString().length())
			{
				String input	= nextInput();
				if(graphInspector.nextHighlight(input))
				{
					jtfAnswer.forward(startPnt, endPnt);
				}
				else
				{
					JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "The current string can not be accepted the automata");
				}						
			}
		}
	}
	
	public String nextInput()
	{
		String 	input, nextInput;
		int 	nextComma;
		
		input		= jtfAnswer.getNetString();
		nextComma	= input.indexOf(",", strPointer);
		
		if(nextComma!=-1)
		{
			nextInput 	= 	input.substring(strPointer, nextComma);
			startPnt	=	strPointer;
			endPnt		=	nextComma;
			strPointer	=	nextComma+1;
			
			return nextInput;
		}
		else
		{
			nextInput	= 	input.substring(strPointer);
			startPnt	=	strPointer;
			endPnt		=	input.length();
			strPointer	= 	input.length();			
			
			return nextInput;
		}
	}
	
	public String previousInput()
	{
		String 	input, previousInput;
		int 	previousComma;
		
		input		= jtfAnswer.getNetString();
		
		strPointer	=	startPnt;
		previousInput	=	input.substring(startPnt, endPnt);
		
		if(startPnt!=0)
		{
			previousComma	=	input.lastIndexOf(",", startPnt-2);
			if(previousComma!=-1)
			{
				endPnt		=	startPnt		-	1;
				startPnt	=	previousComma	+	1;				
			}
			else
			{
				endPnt		=	startPnt		-	1;				
				startPnt	=	0;
			}
		}
		
		return previousInput;
				
		
	}

}
